using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;
using System.Collections.Generic;

public partial class Admin_News : SFPage
{
    busNews insBusNews = new busNews();
    entNews insEntNews = new entNews();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (sfgNews.LoadGridData())
            //{
            //    return;
            //}
            this.Id = 0;
        }
    }
    protected bool sfgNews_NeedsDataSource()
    {
        DataTable insDataTableNews = insBusNews.SelectNews();
        if (insBusNews.HasErrors)
        {
            MessageBoxShow(insBusNews);
        }
        sfgNews.SetData(insDataTableNews);
        return insBusNews.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgNews_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntNews.NewsId = Convert.ToInt32(e.CommandArgument);
            insBusNews.DeleteNewsByNewsId(insEntNews);
            if (insBusNews.HasErrors)
            {
                MessageBoxShow(insBusNews);
                return;
            }
            sfgNews.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstNews.LoadDataListData();
        
    }
    protected bool lstNews_NeedsDataSource()
    {
        insEntNews.NewsId = this.Id;
        insBusNews.SelectNewsByNewsId(insEntNews);
        if (insBusNews.HasErrors)
        {
            MessageBoxShow(insBusNews);
            return true;
        }
        txtNewsDescription.Text = insEntNews.NewsDescription;
        calInsertDate.Value = insEntNews.InsertDate;

        entNewsContents insEntNewsContents = new entNewsContents();
        insEntNewsContents.NewsId = insEntNews.NewsId;
        busNewsContents insBusNewsContents = new busNewsContents();
        DataTable insDataTableNewsContents = insBusNewsContents.SelectNewsContentsByNewsId(insEntNewsContents);
        if (insBusNewsContents.HasErrors)
        {
            MessageBoxShow(insBusNewsContents);
            return true;
        }
        lstNews.SetData(insDataTableNewsContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }
       
            insEntNews.NewsDescription = txtNewsDescription.Text;
            insEntNews.InsertDate = calInsertDate.Value.Value;

        foreach (DataListItem dli in lstNews.Items)
        {
            entNewsContents insEntNewsContents = new entNewsContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntNewsContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntNewsContents.NewsBody = ((SFEditor)dli.FindControl("ediNewsBody")).HTML;
                insEntNewsContents.NewsSummary = ((SFEditor)dli.FindControl("ediNewsSummary")).HTML;
                insEntNewsContents.NewsBodyText = ((SFEditor)dli.FindControl("ediNewsBody")).Text;
                insEntNewsContents.NewsSummaryText = ((SFEditor)dli.FindControl("ediNewsSummary")).Text;
                insEntNewsContents.NewsTitle = ((SFTextBox)dli.FindControl("txtNewsTitle")).Text;
                if (lstNews.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntNewsContents.NewsContentId = Convert.ToInt32(lstNews.DataKeys[dli.ItemIndex]);
                    insEntNews.NewsContentsUpdate.Add(insEntNewsContents);
                }
                else
                {
                    insEntNews.NewsContentsInsert.Add(insEntNewsContents);
                }
            }
            else
            {
                if (lstNews.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntNewsContents.NewsContentId = Convert.ToInt32(lstNews.DataKeys[dli.ItemIndex]);
                    insEntNews.NewsContentsDelete.Add(insEntNewsContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusNews.InsertNews(insEntNews);
        if (insBusNews.HasErrors)
        {
            MessageBoxShow(insBusNews);
            return;
        }
        base.SaveNew();
        sfgNews.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntNews.NewsId = this.Id;
        insBusNews.UpdateNewsByNewsId(insEntNews);
        if (insBusNews.HasErrors)
        {
            MessageBoxShow(insBusNews);
            return;
        }
        base.SaveExisting();
        sfgNews.LoadGridData();
    }

    protected void lstNews_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["NewsContentId"] != DBNull.Value);
                e.Item.FindControl("pnlNewsContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }

    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlNewsContents").Visible = ((SFCheckBox)sender).Checked;
    }
}
